# -*- coding: utf-8 -*-

import pandas as pd
from sklearn.ensemble import RandomForestClassifier


def test_score(test_feature, test_target):

    data = pd.read_csv('credit_card_train.csv', header=0)

    # 对 'SEX', 'EDUCATION', 'MARRIAGE' 列独热编码
    data = pd.get_dummies(data, columns=['SEX', 'EDUCATION', 'MARRIAGE'])

    # 定义列名列表
    columns = list(data.columns)

    # 将 'DEFAULT' 移到最后
    columns.remove('DEFAULT')
    columns.append('DEFAULT')

    # 重新排布列序
    data = data.reindex(columns=columns)

    # 定义特征和目标值
    feature = data.iloc[:, 1:23].values
    target = data['DEFAULT'].values

    # 训练随机森林分类器
    model = RandomForestClassifier()
    model.fit(feature, target)

    # 使用 .score() 方法获得准确率
    score = model.score(test_feature, test_target)

    return score